package com.qiqidream.admin.security.login;

import com.alibaba.fastjson.JSON;
import com.qiqidream.admin.common.response.ResponseCode;
import com.qiqidream.admin.common.response.ResponseResult;
import com.qiqidream.admin.common.utils.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 认证权限入口 - 未登录的情况下访问所有接口都会拦截到此
 * @author QiQiDream
 * @since 2019/10/23 11:00
 */
@Slf4j
@Component
public class AdminAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) {

        ResponseResult failedResult = ResponseResult.failed(ResponseCode.UNAUTHORIZED);
        ResponseUtil.out(response, JSON.toJSONString(failedResult));

    }

}
